/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.types;

import de.tor.tribes.reportserver.PersistenceImpl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

/**
 *
 * @author Torridity
 */
@Entity
@Table(name = "ParserConfigurations")
public class ParserConfiguration {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String serverId;
    @OneToMany(mappedBy = "config", cascade = CascadeType.ALL)
    private Set<ParserPatternRelation> patternRelations;
    @Transient
    private String[] supportedUnits = null;
    @Transient
    private HashMap<String, String> buildingMappings;
    
    public ParserConfiguration() {
        patternRelations = new HashSet<ParserPatternRelation>();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the patternRelations
     */
    public Set<ParserPatternRelation> getPatternRelations() {
        return patternRelations;
    }

    /**
     * @param patternRelations the patternRelations to set
     */
    public void setPatternRelations(Set<ParserPatternRelation> patternRelations) {
        this.patternRelations = patternRelations;
    }

    /**
     * @return the serverId
     */
    public String getServerId() {
        return serverId;
    }

    /**
     * @param serverId the serverId to set
     */
    public void setServerId(String serverId) {
        this.serverId = serverId;
    }
    
    public String getValueByPatternId(final ParserPattern.PARSER_PATTERN_ID pId) {
        ParserPatternRelation rel = (ParserPatternRelation) CollectionUtils.find(patternRelations, new Predicate() {
            public boolean evaluate(Object o) {
                ParserPatternRelation r = (ParserPatternRelation) o;
                return r.getPattern().getPatternName().equals(pId);
            }
        });
        
        if (rel != null) {
            return rel.getPatternValue();
        }
        return pId.getDefault();
    }
    
    public void setPatternValue(final ParserPattern.PARSER_PATTERN_ID pId, String pValue, PersistenceImpl pPersistence) {
        ParserPatternRelation rel = (ParserPatternRelation) CollectionUtils.find(patternRelations, new Predicate() {
            @Override
            public boolean evaluate(Object o) {
                ParserPatternRelation r = (ParserPatternRelation) o;
                return r.getPattern().getPatternName().equals(pId);
            }
        });
        
        if (rel == null) {
            ParserPatternRelation r = new ParserPatternRelation();
            ParserPattern pp = pPersistence.performQueryForSingleValue("SELECT * FROM ParserPatterns WHERE patternname='" + pId.toString() + "'", ParserPattern.class);
            r.setPattern(pp);
            r.setPatternValue(pValue);
            r.setConfig(this);
            pPersistence.persist(r);
            patternRelations.add(r);
        } else {
            rel.setPatternValue(pValue);
        }
    }
    
    public String[] getUnits() {
        if (supportedUnits == null) {
            String v = getValueByPatternId(ParserPattern.PARSER_PATTERN_ID.SUPPORTED_UNITS);
            if (v == null) {
                v = ParserPattern.PARSER_PATTERN_ID.SUPPORTED_UNITS.getDefault();
            }
            supportedUnits = v.split(",");
        }
        return supportedUnits;
    }
    
    public HashMap<String, String> getBuildingMappings() {
        if (buildingMappings == null) {
            buildingMappings = new HashMap<String, String>();
            String v = getValueByPatternId(ParserPattern.PARSER_PATTERN_ID.BUILDING_MAPPINGS);
            if (v == null) {
                v = ParserPattern.PARSER_PATTERN_ID.SUPPORTED_UNITS.getDefault();
            }
            String[] mappings = v.split(",");
            for (String mapping : mappings) {
                String[] building = mapping.trim().split("=");
                buildingMappings.put(building[0], building[1]);
            }
        }
        return buildingMappings;
    }
}
